package com.mine.photohandler.base;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ListView和GridView的万能适配器
 * @param <T>
 */
public abstract class AbsBaseAdapter<T> extends BaseAdapter {

    protected Context context;
    protected List<T> datas;
    private int resid;
    protected boolean isNeedToRemember = false;
    public HashMap<Integer, ItemState> map_isSelected;
    private int clickPosition = -1; // 选中的位置
    private int lastSeletPosition = -1; // 上一个被记录的位置

    /**
     *
     * @param context
     * @param isNeedToRemember 是否需要记录被选中位置
     * @param resid
     */
    public AbsBaseAdapter(Context context, boolean isNeedToRemember,int resid) {
        this.context = context;
        this.resid = resid;
        this.isNeedToRemember = isNeedToRemember;
        datas = new ArrayList();
        if (isNeedToRemember){
            map_isSelected = new HashMap<>();
        }
    }

    public AbsBaseAdapter(Context context,int resid) {
        this.context = context;
        this.resid = resid;
        datas = new ArrayList();
    }


    public HashMap<Integer, ItemState> getMap_isSelected() {
        return map_isSelected;
    }

    public void setMap_isSelected(HashMap<Integer, ItemState> map_isSelected) {
        this.map_isSelected = map_isSelected;
    }

    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < datas.size(); i++) {
            ItemState itemState = new ItemState();
            itemState.setSelected(false);
            itemState.setData(datas.get(i));
            map_isSelected.put(i, itemState);
        }
    }



    public void setDatas(List<T> datas) {
        this.datas = datas;
        //初始化
        if (isNeedToRemember){
            initDate();
        }
        this.notifyDataSetChanged();
    }

    public void addDatas(List<T> datas) {
        this.datas.addAll(datas);
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView != null) {
            viewHolder = (ViewHolder) convertView.getTag();

        } else {
            convertView = LayoutInflater.from(context).inflate(resid, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);

        }

        //进行赋值的操作
        bindDatas(position, convertView, viewHolder, datas.get(position));

        return convertView;
    }


    /**
     * 将布局传出去,动态添加子控件
     *
     * @param convertView
     * @param viewHolder
     * @param data
     */
    public abstract void bindDatas(int position,View convertView, ViewHolder viewHolder, T data);


    /**
     * 创建ViewHolder
     *
     * @author Ken
     */
    public class ViewHolder {
        /**
         * 该Map用于缓存布局中的控件
         */
        Map<Integer, View> cacheMap = new HashMap();
        View layoutView;

        /**
         * 该构造参数 需要传递一个布局对象过来
         *
         * @param layoutView
         */
        public ViewHolder(View layoutView) {
            this.layoutView = layoutView;
        }

        /**
         * 通过控件ID 找到该控件
         *
         * @param id
         * @return
         */
        public <T extends View>T getView(int id) {
            if (cacheMap.containsKey(id)) {
                return (T) cacheMap.get(id);
            } else {
                View view = layoutView.findViewById(id);
                cacheMap.put(id, view);
                return (T) view;
            }
        }


        /**
         * 绑定TextView
         *
         * @param id
         * @param text
         * @return
         */
        public ViewHolder bindTextView(int id, String text) {
            TextView tv = (TextView) getView(id);
            tv.setText(text);
            return this;
        }



        /**
         * 绑定多种颜色的TextView
         *
         * @param id
         * @param text
         * @return
         */
        public ViewHolder bindDifferentColorTextView(int id, String text) {
            TextView tv = (TextView) getView(id);
            tv.setText(Html.fromHtml(text));
            return this;
        }

//        /**
//         * 绑定CacheImageView
//         *
//         * @param id
//         * @param url
//         * @return
//         */
//        public ViewHolder bindImageView(int id, String url) {
//            ImageView iv = (ImageView) getView(id);
//            Glide.with(context)
//			.load(url)
//			.centerCrop()
//			.into(iv);
//            return this;
//        }
        
        /**
         * 绑定Listview
         * @param id
         * @param adpter
         * @return
         */
        public ViewHolder bindListView(int id,BaseAdapter adpter){
        	ListView lv = (ListView) getView(id);
            lv.setAdapter(adpter);
            return this;
        }
        
        
        /**
         * 绑定Listview
         * @param id
         * @param
         * @return
         */
        public ViewHolder bindGridView(int id,BaseAdapter adapter){
        	GridView gv = (GridView) getView(id);
        	gv.setAdapter(adapter);
            return this;
        }

        /**
         * ImageView
         *
         * @param id
         * @param resId
         * @return
         */
        public ViewHolder bindImageView(int id, int resId) {
            ImageView iv = (ImageView) getView(id);
            iv.setImageResource(resId);
            return this;
        }

        /**
         * ImageView
         *
         * @param id
         * @param bitmap
         * @return
         */
        public ViewHolder bindImageView(int id, Bitmap bitmap) {
            ImageView iv = (ImageView) getView(id);
            iv.setImageBitmap(bitmap);
            return this;
        }

    }

    public int getClickPosition() {
        return clickPosition;
    }

    public void setClickPosition(int clickPosition) {
        this.clickPosition = clickPosition;
    }

    public int getLastSeletPosition() {
        return lastSeletPosition;
    }

    public void setLastSeletPosition(int lastSeletPosition) {
        this.lastSeletPosition = lastSeletPosition;
    }

    /**
     * 用于记录当前状态数据
     * @param <T>
     */
    public static class ItemState<T> {
        private boolean isSelected;
        private T data;

        public boolean isSelected() {
            return isSelected;
        }

        public void setSelected(boolean selected) {
            isSelected = selected;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }
    }
    /**
     * 清出所有状态
     */
    public void cleanAllState(){
        lastSeletPosition = -1;
        initDate();
    }
    /**
     * 记录某个数据状态
     */
    public void changeSel(int position){
        lastSeletPosition = position;
        map_isSelected.get(position).setSelected(true);
        this.notifyDataSetChanged();
    }
    /**
     * 清出某个数据状态
     */
    public void cleanSel(int position){
        if (position == -1){
            return;
        }
        if (lastSeletPosition == position){
            lastSeletPosition = -1;
        }
        map_isSelected.get(position).setSelected(false);
        this.notifyDataSetChanged();
    }

    /**
     * 全选
     */
    public void selectAllState(int position){
        lastSeletPosition = datas.size()-1;
        for (int i = 0; i < datas.size(); i++) {
            ItemState itemState = new ItemState();
            itemState.setSelected(true);
            itemState.setData(datas.get(i));
            map_isSelected.put(i, itemState);
        }
    }

}
